<template>
	<view>
		<!-- 切换 -->
		<u-sticky>
			<view class="lb-tabs">
				<view class="tabs-item" :class="tabsIndex == index?'active':''" v-for="(item,index) in tabsData"
					:key="index" @click="clickTabs(index)">
					<view class="text">
						<view class="name">
							{{item.name}}
						</view>
						<view class="numb" :class="index == 0?'numb-jia':''">
							{{item.numb}}
						</view>
					</view>
					<view class="btn">

					</view>
				</view>
			</view>
			<!-- 搜索 -->
			<view class="lb-search">
				<u-search height="80rpx" bgColor="#fff" placeholderColor="#333" color="#333" placeholder="搜索"
					@clear="search" borderColor="#E7E7E7" v-model="keyword" :showAction="false"
					@change="search"></u-search>
			</view>
		</u-sticky>

		<!-- 列表 -->
		<u-empty v-if="listData.length < 1" mode="data" text="暂无数据" icon="/static/empty/data.png"></u-empty>
		<view class="lb-box" v-for="(item,index) in listData" :key="index">
			<view class="lb-item">
				<view class="lb-left">
					<u-avatar :src="getImgUrl(item.avatar)" size="80rpx"></u-avatar>
				</view>
				<view class="lb-right">
					<view class="lb-name">
						{{item.name?item.name:"匿名用户"}}
					</view>
					<view class="lb-time">
						{{item.createtime}}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		distributionApiClient
	} from "@/api/apis/retail.js"
	import {
		setImgUrl
	} from "@/utils/comm.js"
	export default {
		data() {
			return {
				tabsIndex: 0,
				tabsData: [{
						name: '今日新增',
						numb: 0
					},
					{
						name: '本月新增',
						numb: 0
					},
					{
						name: '客户总数',
						numb: 0
					},
				],
				keyword: '',
				page: 1,
				limit: 10,
				listData: [],
				isLoading: false
			}
		},

		methods: {
			//切换
			clickTabs(i) {
				this.tabsIndex = i;
				this.page = 1;
				this.getList();
			},
			//跳转
			goTo(url) {
				if (url) {
					uni.navigateTo({
						url: url
					})
				}
			},
			//搜索
			search() {
				this.page = 1;
				this.getList();
			},
			getImgUrl(url) {
				return setImgUrl(url);
			},
			//列表
			getList() {
				uni.showLoading({
					title: "加载中",
					mask: true
				});
				this.isLoading = true;
				distributionApiClient({
					// is_recommend: 1,
					type: this.tabsIndex + 1,
					page: this.page,
					limit: this.limit,
					keywords: this.keyword
				}).then(res => {
					uni.hideLoading();
					let data = res.data;
					if (this.page == 1) {
						this.tabsData[0].numb = data.today;
						this.tabsData[1].numb = data.month;
						this.tabsData[2].numb = data.total_num;
						this.listData = data.list.data;
					} else {
						this.listData = this.listData.concat(data.list.data);
					}
					if (data.list.total > this.page * this.limit) {
						// console.log(res.data.length)
						//请求成功 节流阀 设置为false（没有走完函数不允许再次请求）
						this.isLoading = false;
						this.page++;
					}
				})
			},
		},
		onLoad() {
			this.getList();
		},
		onReachBottom() {
			//注意 这里是重要的一步，不满足条件下拉加载不能继续请求数据
			if (!this.isLoading) {
				this.getList();
			}

		},
	}
</script>

<style lang="scss">
	.lb-tabs {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		background-color: #FFFFFF;

		.tabs-item {
			width: calc(100% - 2rpx);
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: space-between;
			box-sizing: border-box;
			border-bottom: 1rpx solid #F5F5F5;



			.text {
				font-size: $main-font-28;
				color: $main-font-color;
				font-weight: bold;
				padding: 24rpx 0 12rpx;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;

				.name {}

				.numb {}

				.numb-jia {
					&::before {
						content: '+';
					}
				}


			}

			.btn {
				height: 0;
				width: 0;
				border-width: 10rpx;
				border-style: solid;
				border-color: transparent transparent transparent transparent; //分别为上右下左
			}
		}

		.active {
			border-bottom: 2rpx solid #00D26E;

			.text {
				color: #00D26E;

			}

			.btn {
				border-color: transparent transparent #00D26E transparent; //分别为上右下左
			}
		}

	}

	//搜索
	.lb-search {
		padding: 32rpx;
		background-color: #FFFFFF;
	}

	//列表
	.lb-box {
		display: flex;
		background: rgba(255, 255, 255, 1);
		border-top: 2rpx solid #F5F5F5;
		padding: 24rpx 32rpx;
		box-sizing: border-box;

		.lb-item {
			width: 100%;
			display: flex;
			align-items: center;

			.lb-left {}

			.lb-right {
				display: flex;
				width: calc(100% - 80rpx);
				justify-content: space-between;
				align-items: center;
				padding-left: 24rpx;

				.lb-name {
					font-size: $main-font-28;
					color: $main-font-color;
				}

				.lb-time {
					font-size: $main-font-28;
					color: $main-font-color-grey9;
				}
			}
		}

	}
</style>